package com.xxx.zkconfig.helper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.util.Map;
import java.util.Properties;

/**
 * Created by louisyuu on 2020/4/29 上午10:54
 */
public class ZkUtils {

    private final static String DEFAULT_CONNECT_ADDRESS = "127.0.0.1:2181";

    public final static String DEFAULT_NAMESPACE = "application-config";

    private static CuratorFramework globalZkClient;


    private ZkUtils() {
    }

    public static CuratorFramework getZkClient() {
        return globalZkClient;
    }


    static {
        initGlobalZkClient();
    }


    private static void initGlobalZkClient() {
        globalZkClient = createSession(DEFAULT_CONNECT_ADDRESS, DEFAULT_NAMESPACE);
    }


    public static CuratorFramework createSession(String connectAddress, String namespace) {
        RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);

        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString(connectAddress)
                .sessionTimeoutMs(50000)
                .connectionTimeoutMs(30000)
                .retryPolicy(exponentialBackoffRetry)
                .namespace(namespace)  // 独立的命名空间 /base
                .build();

        client.start();
        return client;


    }


    public static Map<String, Object> readConfigToMap() {
        FastJsonSerializer fastJsonSerializer = new FastJsonSerializer();
        try {
            byte[] bytes = ZkUtils.getZkClient().getData().forPath("/");
            Map<String, Object> propFromZk = fastJsonSerializer.deserialize(Map.class, bytes);
            return propFromZk;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


    public static Properties readConfigToProperties() {
        Map<String, Object> map = readConfigToMap();
        Properties properties = new Properties();
        properties.putAll(map);
        return properties;

    }


    public static Map<String, Object> initConfigIfEmpty() {
        try {
            FastJsonSerializer fastJsonSerializer = new FastJsonSerializer();
            Map<String, Object> configMap = readConfigToMap();
            if (configMap == null || configMap.isEmpty()) {
                Map<String, Object> prop = PropertyReader.getCache();
                ZkUtils.getZkClient().setData().forPath("/", fastJsonSerializer.serialize(prop));
            }
            return readConfigToMap();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


    public static void updateConfig(Map<String, Object> config) {
        try {
            FastJsonSerializer fastJsonSerializer = new FastJsonSerializer();
            ZkUtils.getZkClient().setData().forPath("/", fastJsonSerializer.serialize(config));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
